<script setup>
import {ref} from "vue";
import {useForm} from "@inertiajs/vue3";
import useDialog from '@/composables/useDialog'
import useLoading from '@/composables/useLoading'
import useRequest from '@/composables/useRequest'
import {Message} from "@arco-design/web-vue";

const {FormRef, visible, mode, pk, actionName, width} = useDialog()
const {loading, setLoading} = useLoading()

const {createApi, updateApi, showApi} = useRequest('admin.setting.wechat.messages')

const emit = defineEmits(["success", "close"]);

const form = useForm({
    key: '',
    msg_type: 'text',
    content: '',
    extra_json: [],
    status: 1,
    sort:50
})

const TypeList = ref([
    {
        label: '文本',
        value: 'text'
    },
    {
        label: '关注',
        value: 'subscribe'
    },
    {
        label: '默认',
        value: 'default'
    },
])
const changeType = (type) => {
    if (type === 'text') {
        form.key = ''
    }else{
        form.key = type
    }

}
const rules = {
    key: [{required: true, message: '请输入消息关键词', trigger: 'blur'}],
    msg_type: [{required: true, message: '请输入消息回复类型', trigger: 'blur'}],
    content: [{required: true, message: '请输入消息回复内容', trigger: 'blur'}],
    sort: [{required: true, message: '请输入消息回复排序', trigger: 'blur'}],
    status: [{required: true, message: '请输入消息回复状态', trigger: 'blur'}],
}


const handleSubmit = async () => {
    try {
        setLoading(true)
        const res = await FormRef.value?.validate()
        if (res) return false
        const apiMethod = mode.value === 'edit'
            ? () => updateApi(pk.value, form)
            : () => createApi(form)
        await apiMethod()
        Message.success(`${actionName.value}成功`)
        emit('success')
    } catch (error) {
        return false
    } finally {
        setLoading(false)
    }
}

const handleClose = () => {
    FormRef.value?.resetFields()
    form.reset()
    emit('close')
}

// 打开弹窗
const add = async () => {
    mode.value = 'add'
    visible.value = true
}

const edit = async (id) => {
    try {
        setLoading(true)
        pk.value = id
        const {data} = await showApi(id)
        Object.assign(form, data)
        mode.value = 'edit'
        visible.value = true
    } finally {
        setLoading(false)
    }
}

defineExpose({add, edit})
</script>
<template>
    <a-drawer
        v-model:visible="visible"
        :loading="loading"
        :title="`账号${actionName}`"
        :width
        @close="handleClose"
        @before-ok="handleSubmit"
    >
        <a-spin :loading="loading" class="w-full">


            <a-form ref="FormRef" :model="form" :rules auto-label-width>

                <a-form-item field="msg_type" label="消息回复类型">
                    <a-select
                        v-model="form.msg_type"
                        :field-names="{
                        label: 'label',
                        value: 'value'
                    }"
                        :options="TypeList"
                        placeholder="请选择消息回复类型"
                        @change="changeType"
                    />
                </a-form-item>

                <a-form-item v-if="form.msg_type.includes('text')" field="key" label="消息关键词">
                    <a-input
                        v-model.trim="form.key"
                        allow-clear
                        placeholder="请输入消息关键词"
                    />
                </a-form-item>



                <a-form-item field="content" label="消息回复内容">
                    <a-input
                        v-model="form.content"
                        allow-clear
                        placeholder="请输入消息回复内容"
                    />
                </a-form-item>

                <a-form-item field="status" label="状态">
                    <a-radio-group v-model="form.status">
                        <a-radio :value="1">显示</a-radio>
                        <a-radio :value="0">隐藏</a-radio>
                    </a-radio-group>
                </a-form-item>

                <a-form-item field="sort" label="排序">
                    <a-input-number v-model="form.sort" :min="1" :step="10" mode="button" placeholder="请输入排序"/>
                </a-form-item>
            </a-form>
        </a-spin>
    </a-drawer>
</template>

<style scoped>

</style>
